package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.entity.TrainCommitRecord;
import com.example.demo.enums.TrainEvalStatusEnum;
import com.example.demo.service.TrainCommitRecordService;
import com.example.demo.mapper.TrainCommitRecordMapper;
import com.example.demo.utils.CommonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author DaoJia
* @description 针对表【train_commit_record(培训系统-提交记录)】的数据库操作Service实现
* @createDate 2024-02-05 21:01:57
*/
@Service
public class TrainCommitRecordServiceImpl extends ServiceImpl<TrainCommitRecordMapper, TrainCommitRecord>
    implements TrainCommitRecordService{


    @Resource
    private TrainCommitRecordMapper trainCommitRecordMapper;

    @Override
    public List<TrainCommitRecord> queryByHwIdAndPIdAndClassId(Long homeworkId, Long problemId, Long classId) {
        QueryWrapper<TrainCommitRecord> queryWrapper = new QueryWrapper<>();
        if (CommonUtil.isNotNullId(homeworkId)){
            queryWrapper.eq("homework_id", homeworkId);
        }
        if (CommonUtil.isNotNullId(problemId)){
            queryWrapper.eq("problem_id", problemId);
        }
        if (CommonUtil.isNotNullId(classId)){
            queryWrapper.eq("class_id", classId);
        }
        List<TrainCommitRecord> trainCommitRecords = trainCommitRecordMapper.selectList(queryWrapper);
        return trainCommitRecords;
    }

    @Override
    public List<TrainCommitRecord> queryByHwIdAndPIdAndClassIdAndUser(Long homeworkId, Long problemId, Long classId, Long userId) {
        QueryWrapper<TrainCommitRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("homework_id", homeworkId)
                .eq("problem_id", problemId)
                .eq("class_id", classId)
                .eq("user_id", userId)
                .orderByDesc("submit_time");
        List<TrainCommitRecord> trainCommitRecords = trainCommitRecordMapper.selectList(queryWrapper);
        return trainCommitRecords;
    }

    @Override
    public TrainCommitRecord getBySubmissionId(Long submissionId) {
        QueryWrapper<TrainCommitRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("submit_id", submissionId);
        return trainCommitRecordMapper.selectOne(queryWrapper);
    }

    @Override
    public void changeScore(Long userId, Long submitId, Integer score) {
        QueryWrapper<TrainCommitRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("submit_id", submitId);
        queryWrapper.eq("user_id", userId);
        TrainCommitRecord record = trainCommitRecordMapper.selectOne(queryWrapper);
        record.setEvalStatus(TrainEvalStatusEnum.HANG_DONE.getKey());
        record.setScore(score);
        trainCommitRecordMapper.updateById(record);
    }

    @Override
    public List<TrainCommitRecord> getByClassIdAndProblemId(Long classId, Long problemId) {
        QueryWrapper<TrainCommitRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("problem_id", problemId)
                .eq("class_id", classId);
        return trainCommitRecordMapper.selectList(queryWrapper);
    }
}




